package com.seecen.HJM.mapper;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Constants;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.seecen.HJM.model.entity.Song;
import com.seecen.HJM.model.vo.SongAdminVo;
import com.seecen.HJM.model.vo.SongDetailVo;
import com.seecen.HJM.model.vo.SongStyleAndCountVo;
import com.seecen.HJM.model.vo.SongVo;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * @author TOYA
 * @description 针对表【tb_song】的数据库操作Mapper
 * @createDate 2025-09-26 14:39:18
 * @Entity com.seecen.HJM.model.entity.Song
 */
public interface SongMapper extends BaseMapper<Song> {


    //获取歌曲列表
    IPage<SongVo> getSongsWithArtist(Page<SongVo> page,
                                     @Param("songName") String songName,
                                     @Param("artistName") String artistName,
                                     @Param("album") String album);



    //获取随机歌曲列表
    @Select(
            """
                    select 
                        s.id as songId,
                        s.name as songName,
                        s.album,
                        s.duration,
                        s.cover_url as coverUrl,
                        s.audio_url as audioUrl,
                        s.release_time as releaseTime,
                        a.name as artistName
                        from tb_song s 
                    left join tb_artist a on s.artist_id=a.id
                    order by RAND() limit 20
                    """
    )
    List<SongVo> getRandomSongsWithArtist();

    // 根据用户收藏的歌曲id列表获取歌曲列表
    List<Long> getFavoriteSongStyles(@Param("favoriteSongIds") List<Long> favoriteSongIds);

    // 根据用户收藏的歌曲id列表获取歌曲列表
    List<SongVo> getRecommendedSongsByStyles(@Param("sortedStyleIds") List<Long> sortedStyleIds,
                                             @Param("favoriteSongIds") List<Long> favoriteSongIds,
                                             @Param("limit") int limit);

    // 根据id获取歌曲详情
    SongDetailVo getSongDetailById(Long songId);

    // 根据歌手获取歌曲列表
    @Select("""
                SELECT 
                    s.id AS songId, 
                    s.name AS songName, 
                    s.artist_id AS artistId, 
                    s.album, 
                    s.lyric, 
                    s.duration, 
                    s.style, 
                    s.cover_url AS coverUrl, 
                    s.audio_url AS audioUrl, 
                    s.release_time AS releaseTime, 
                    a.name AS artistName
                FROM tb_song s
                LEFT JOIN tb_artist a ON s.artist_id = a.id
                WHERE 
                    (#{artistId} IS NULL OR s.artist_id = #{artistId})
                    AND(#{songName} IS NULL OR s.name LIKE CONCAT('%', #{songName}, '%'))
                    AND (#{album} IS NULL OR s.album LIKE CONCAT('%', #{album}, '%'))
                    AND (#{style} IS NULL OR s.style LIKE CONCAT('%', #{style}, '%'))
                ORDER BY s.release_time DESC
            """)
    IPage<SongAdminVo> getSongsWithArtistName(Page<SongAdminVo> page,
                                              @Param("artistId") Long artistId,
                                              @Param("songName") String songName,
                                              @Param("album") String album,
                                              @Param("style") String style);


    //获取歌曲风格和数量的键值对对象
    List<SongStyleAndCountVo>getSongStyleAndCountVo();


    // 根据用户收藏的歌曲id列表获取歌曲列表
    IPage<SongVo> getSongsByIds(Page<SongVo> page,
                                @Param("songIds") List<Long> songIds,
                                @Param("songName") String songName,
                                @Param("artistName") String artistName,
                                @Param("album") String album);
    //获取用户喜欢的歌曲
    IPage<Song> getLikedSongsPage(IPage<Song> page1,@Param(Constants.WRAPPER) QueryWrapper<Song> queryWrapper1);



}




